<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE); //屏蔽错误
/**
 * 对象转数组
 * @param $obj
 * @return mixed
 */
function obj2array($obj){
    return json_decode(json_encode($obj),true);
}
/**
 *生成18位不重复单据号
 * @return string 生成好的单据号
 */
function rand_number()
{
    static $ORDERSN = [];
    $ors = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);
    if (isset($ORDERSN[$ors])) {
        $ORDERSN[$ors]++;
    } else {
        $ORDERSN[$ors] = 1;
    }
    return $ors . str_pad($ORDERSN[$ors], 2, '0', STR_PAD_LEFT);
}

function sentMsg($opt){
    $message = new dingtalk\Message();
    $res = $message::sendToConversation(cache('corp_access_token'), $opt);
    if($res['errcode'] == 0){
        return true;
    }
    $this->error = $res['errmsg'];
    return false;
}
/**
 * 获取指定表指定字段值
 * @param string $table 指定表
 * @param string $field 字段名
 * @param string|array $map 条件
 * @return string 获取的值
 */
function get_field_value($table, $field, $map)
{
    return \think\Db::name($table)->where($map)->value($field);
}
/**
 * 字符串截取，支持中文和其他编码
 * @param string $str 源字符串
 * @param int $start 从哪里开始
 * @param int $length 截取的长度
 * @param string $charset 编码，默认utf-8
 * @param bool $suffix 尾部是否添加···,默认添加
 * @return bool|string 截取好的字符串
 */
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    if ($suffix and mb_strlen($str, 'utf8') > $length) {
        return $slice . '...';
    } else {
        return $slice;
    }
}
